<?php

class dbt_coupon extends sing_table
{
    private $_G;
    private $_mid;
    private $_openid;
    private $_memberId;

    //构造方法
    function __construct(){
        global $_G;
        $this->_G = &$_G;
        $this->_mid = $_GET['mid'];
        $this->_openid =  &$this->_G[$this->_mid]['openid'];
        $this->_memberId = &$this->_G[$this->_mid]['memberid'];
    }
    
    //公共查询
    public function pubFetch($table,$field="*",$where="",$type="all",$limit="",$SetMainDB=DB_MAIN){

        DB::SetMainDB($SetMainDB);
        switch($type) {
            case 'first':
                $data = DB::fetch_first('select %i from %t %i',array($field,$table,$where));
                break;
            case 'result':
                $data = DB::result_first('select %i from %t %i',array($field,$table,$where));
                break;
            default:
                $data = DB::fetch_all('select %i from %t %i %i',array($field,$table,$where,$limit));
                break;
        }
        return $data;
    }
    
    //查询优惠券记录
    public function couponFetch($field="*",$where="",$type="all",$limit=""){

        $lastWhere = is_int(strpos($where,"where")) ? $where : DB::format(" where display=1 and mid=%s", array($this->_mid)).$where;
        switch($type) {
            default:
                $data = $this->pubFetch("es_coupon",$field,$lastWhere,$type,$limit,DB_ESHOP);
        }
        return $data;
    }
    
    //查询优惠券领取记录
    public function couponLogFetch($field="*",$where="",$type="all",$limit=""){

        $lastWhere = is_int(strpos($where,"where")) ? $where : DB::format(" where display=1 and mid=%s", array($this->_mid)).$where;
        switch($type) {
            default:
                $data = $this->pubFetch("es_coupon_log",$field,$lastWhere,$type,$limit,DB_ESHOP);
        }
        return $data;
    }
    
    //查询优惠券门店
    public function couponOutletFetch($field="*",$where="",$type="all",$limit=""){

        $lastWhere = is_int(strpos($where,"where")) ? $where : DB::format(" where display=1 and mid=%s", array($this->_mid)).$where;
        switch($type) {
            default:
                $data = $this->pubFetch("es_coupon_log_outlet",$field,$lastWhere,$type,$limit,DB_ESHOP);
        }
        return $data;
    }
    
    //查询优惠券企业关联信息
    public function couponEnterpriseFetch($field="*",$where="",$type="all",$limit=""){

        $lastWhere = is_int(strpos($where,"where")) ? $where : DB::format(" where display=1 and mid=%s", array($this->_mid)).$where;
        switch($type) {
            default:
                $data = $this->pubFetch("es_coupon_enterprise",$field,$lastWhere,$type,$limit,DB_ESHOP);
        }
        return $data;
    }
    //查询优惠券企业关联信息
    public function enterpriseFetch($field="*",$where="",$type="all",$limit=""){

        $lastWhere = is_int(strpos($where,"where")) ? $where : DB::format(" where display=1 and mid=%s", array($this->_mid)).$where;
        switch($type) {
            default:
                $data = $this->pubFetch("activity_enterprise",$field,$lastWhere,$type,$limit,DB_ESHOP);
        }
        return $data;
    }
    //查询优惠券企业关联信息
    public function getMemberEnterprise($memberId){
        DB::SetMainDB(DB_ESHOP);
        $data = DB::fetch_first('select * from %t where member_id = %d ',array('es_member_enterprise',$memberId));;

        return $data;
    }
    public function setMemberEnterprise($memberId,$id,$name){

        DB::SetMainDB(DB_ESHOP);
        $result = DB::insert("es_member_enterprise", array('member_id'=>$memberId,'enterprise_id'=>$id,'name'=>$name,'inputtime'=>TIME),true);

        return $result;
    }
    //插入优惠券门店
    public function couponOutletInsert($data=array(),$bool=false){
        if(!$data || !is_array($data)){
            return false;
        }

        $data['mid'] = $this->_mid;
        $data['member_id'] = $this->_memberId;
        $data['openid'] = $this->_openid;
        $data['inputtime'] = TIME;

        DB::SetMainDB(DB_ESHOP);
        $thisId = DB::insert("es_coupon_log_outlet",$data,$bool);
        return $thisId;
    }
    
     //修改优惠券门店
    public function couponOutletUpdate($data=array(),$where=""){
        if(!$data || !is_array($data) || !$where){
            return false;
        }

        $data['updatetime'] = TIME;
        
        DB::SetMainDB(DB_ESHOP);
        $num = DB::update("es_coupon_log_outlet",$data,$where);

        return $num;
    }
}